package com.googlecode.todo_robot.frontend.server.service;

import com.google.gwt.user.server.rpc.RPCServletUtils;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.todo_robot.frontend.client.model.Todo;
import com.googlecode.todo_robot.frontend.client.service.TodoGwtService;
import com.googlecode.todo_robot.persistence.DbManager;
import com.googlecode.todo_robot.persistence.DbManagerImpl;
import com.googlecode.todo_robot.persistence.model.TodoItem;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author Fuad Ibrahimov
 * @since 27.12.2009
 */
public class TodoGwtServiceImpl extends RemoteServiceServlet implements TodoGwtService {
  private DbManager dbManager = new DbManagerImpl();
  private TodoConverter todoConverter = new TodoConverter();

  @Override
  public List<Todo> todosOf(String user) {
    ArrayList<Todo> todos = new ArrayList<Todo>();
    for (TodoItem todoItem : dbManager.loadTodosOf(user)) {
      todos.add(todoConverter.convert(todoItem));
    }
    return todos;
  }

  @Override
  protected String readContent(HttpServletRequest request) throws ServletException, IOException {
    // need to overwrite it for gadgets
    return RPCServletUtils.readContentAsUtf8(request, false);
  }

  private static final Logger logger = Logger.getLogger(TodoGwtServiceImpl.class.getName());

}